﻿package test.ui {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import gear.core.Game;
	import gear.log4a.Logger;
	import gear.net.AssetData;
	import gear.net.LibData;
	import gear.net.RESManager;
	import gear.net.SWFLoader;
	import gear.ui.controls.GButton;
	import gear.ui.controls.GGrid;
	import gear.ui.controls.GPageControl;
	import gear.ui.core.ScaleMode;
	import gear.ui.data.GButtonData;
	import gear.ui.data.GGridData;
	import gear.ui.data.GPageControlData;
	import gear.ui.model.SyncPageModel;
	import gear.ui.skin.tb.MCToggleButtonSkin;


	/**
	 * @version 20091201
	 * @author bright
	 */
	[SWF(width=1000,height=570,backgroundColor=0x333333,frameRate=30)]
	public class TestGGrid extends Game {
		private var _grid : GGrid;
		private var _pc : GPageControl;
		private var _test_btn : GButton;

		override protected function startup() : void {
			_res.add(new SWFLoader(new LibData("assets/ui.swf")));
			_res.addEventListener(Event.COMPLETE, completeHandler);
			_res.load();
		}

		private function completeHandler(event : Event) : void {
			addGrid();
			addPC();
			addButtons();
			initEvents();
		}

		private function addGrid() : void {
			var data : GGridData = new GGridData();
			data.x = 10;
			data.y = 10;
			data.width = 200;
			data.padding = 3;
			data.scaleMode = ScaleMode.AUTO_SIZE;
			data.cell = TestCell;
			data.cellData.skin = new MCToggleButtonSkin(RESManager.getMC(new AssetData("testCell")));
			data.cellData.scaleMode = ScaleMode.NONE;
			data.parent = this;
			data.columns = 2;
			data.rows = 2;
			_grid = new GGrid(data);
			addChild(_grid);
			Logger.debug(_grid.x, _grid.y);
		}

		private function addPC() : void {
			var data : GPageControlData = new GPageControlData();
			data.x = 10;
			data.y = 190;
			_pc = new GPageControl(data);
			addChild(_pc);
		}

		private function addButtons() : void {
			var data : GButtonData = new GButtonData();
			data.x = 110;
			data.y = 220;
			data.labelData.text = "测试";
			_test_btn = new GButton(data);
			addChild(_test_btn);
		}

		private function initEvents() : void {
			_grid.pageModel = new SyncPageModel(TestSyncPageData);
			_pc.model = _grid.pageModel;
			_grid.pageModel.firstPage();
			_test_btn.addEventListener(MouseEvent.CLICK, testClickHandler);
		}

		private function testClickHandler(event : MouseEvent) : void {
			if (_grid.parent != null) {
				_grid.hide();
			} else {
				_grid.show();
			}
		}

		public function TestGGrid() {
			super();
		}
	}
}
